package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.BaseDao.BaseDao;
import com.atguigu.BaseService.impl.BaseServiceImpl;
import com.atguigu.dao.DictDao;
import com.atguigu.dao.HouseDao;
import com.atguigu.entity.House;
import com.atguigu.service.HouseService;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Service(interfaceClass = HouseService.class)
@Transactional
public class HouseServiceImpl extends BaseServiceImpl<House> implements HouseService {
    @Autowired
    private HouseDao houseDao;

    @Autowired
    private DictDao dictDao;

    @Override
    protected BaseDao<House> getEntityDao() {

        return houseDao;
    }


    @Override
    public House getById(Serializable id) {
        House house = houseDao.getById(id);

        //户型：
        String houseTypeName = dictDao.getNameById(house.getHouseTypeId());
        //楼层
        String floorName = dictDao.getNameById(house.getFloorId());
        //建筑结构：
        String buildStructureName = dictDao.getNameById(house.getBuildStructureId());
        //朝向：
        String directionName = dictDao.getNameById(house.getDirectionId());
        //装修情况：
        String decorationName = dictDao.getNameById(house.getDecorationId());
        //房屋用途：
        String houseUseName = dictDao.getNameById(house.getHouseUseId());

        house.setHouseTypeName(houseTypeName);

        house.setFloorName(floorName);

        house.setBuildStructureName(buildStructureName);

        house.setDirectionName(directionName);

        house.setDecorationName(decorationName);

        house.setHouseUseName(houseUseName);

        return house;
    }

    @Override
    public PageInfo<HouseVo> findListPage(Integer pageNum, Integer pageSize, HouseQueryVo houseQueryVo) {

        //分页
        PageHelper.startPage(pageNum, pageSize);
        //调用dao曾
        Page<HouseVo> page = houseDao.findListPage(houseQueryVo);

        List<HouseVo> houseVoList = page.getResult();
        for (HouseVo houseVo : houseVoList) {
            houseVo.setHouseTypeName(dictDao.getNameById(houseVo.getHouseTypeId()));
            houseVo.setFloorName(dictDao.getNameById(houseVo.getFloorId()));
            houseVo.setDirectionName(dictDao.getNameById(houseVo.getDirectionId()));
        }

        //返回
        return new PageInfo<>(page.getResult());
    }

    @Override
    public HouseVo getHouseVoById(Long houseId) {
       HouseVo houseVo =  houseDao.getHouseVoById(houseId);
        return houseVo;
    }
}
